package com.itheima.server.controller;

import com.itheima.dubbo.api.CommentsApi;
import com.itheima.model.mongo.Movement;
import com.itheima.model.vo.PageResult;
import com.itheima.model.vo.VisitorsVo;
import com.itheima.server.service.CommentsService;
import com.itheima.server.service.MovementService;
import com.itheima.server.service.TanhuaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/movements")
public class MovementController {
    @Autowired
    private MovementService movementService;
    @Autowired
    private CommentsService commentsService;
    @Autowired
    private TanhuaService tanhuaService;

    /*动态发布
     * movement是动态具体内容，imagecontent是发布的图片视频之类的东西
     * 参数是表单格式
     * */
    @PostMapping
    public ResponseEntity movements(Movement movement, MultipartFile imageContent[]) throws IOException {
        movementService.publishMovement(movement, imageContent);
        return ResponseEntity.ok(null);
    }

    /*
    用户动态
     * */
    @GetMapping("/all")
    public ResponseEntity all(Long userId, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = movementService.findByUserId(userId, page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /*好友动态*/
    @GetMapping
    public ResponseEntity movements(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = movementService.findFriendMovements(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /*推荐动态*/
    @GetMapping("/recommend")
    public ResponseEntity recommend(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = movementService.findRecommendMovements(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /*单条动态*/
    @GetMapping("/{id}")
    public ResponseEntity id(@PathVariable String id) {
        return ResponseEntity.ok(movementService.findById(id));
    }

    /*动态点赞*/
    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable String id) {
        Integer likeCount = commentsService.likeComment(id);
        return ResponseEntity.ok(likeCount);
    }

    /*动态取消点赞*/
    @GetMapping("/{id}/dislike")
    public ResponseEntity disLike(@PathVariable String id) {
        Integer likeCount = commentsService.disLikeComment(id);
        return ResponseEntity.ok(likeCount);
    }
    /*动态喜欢*/
    @GetMapping("/{id}/love")
    public ResponseEntity love(@PathVariable String id) {
        Integer likeCount = commentsService.loveComment(id);
        return ResponseEntity.ok(likeCount);
    }

    /*动态取消喜欢*/
    @GetMapping("/{id}/unlove")
    public ResponseEntity unLove(@PathVariable String id) {
        Integer likeCount = commentsService.unLoveComment(id);
        return ResponseEntity.ok(likeCount);
    }


    /*谁看过我*/
    @GetMapping("visitors")
    public ResponseEntity visitors() {
        List<VisitorsVo> visitorsVos = commentsService.queryVisitorsList();
        return ResponseEntity.ok(visitorsVos);
    }

}
